using System;
using System.Collections.Generic;
using UnityEngine;

public class PawnBuilder : MonoBehaviour
{
    public static Transform CharaParent;
    /*  public static Card InitCardByCardData(CardData data)
     {
         Card card = new Card();
         card.Attack = data.Attack;
         card.AttackSpeed = data.AttackSpeed;
         card.Defence = data.Defence;
         card.Range = data.Range;
         card.HealPoint = data.HealPoint;
         card.Speed = data.Speed;

         foreach (var feature in data.Features)
         {
             card.Features.Add(Instantiate(feature));
         }
         foreach (var feature in data.RootFeatures)
         {
             card.RootFeatures.Add(Instantiate(feature));
         }

         card.CurrentPawn = Instantiate(data.PawnModelPrefab, CharaParent);
         card.CurrentPawn.Card = card;

         return card;
     } */

    public static Pawn InitPawnByPrefab(Pawn prefab)
    {
        var pawn = Instantiate(prefab, CharaParent);
        pawn.Card = new Card();
        var data = pawn.TestCardData;
        var card = pawn.Card;
        card.Attack = data.Attack;
        card.AttackSpeed = data.AttackSpeed;
        card.Defence = data.Defence;
        card.Range = data.Range;
        card.HealPoint = data.HealPoint;
        card.Speed = data.Speed;
        card.Description = data.Description;
        card.ifHero = data.ifHero;

        for (int i = 0; i < data.Features.Count; i++)
        {
            if (data.Features[i] == null)
                continue;
            var newFeature = Instantiate(data.Features[i]);
            card.Features.Add(newFeature);
        }
        for (int i = 0; i < data.RootFeatures.Count; i++)
        {
            if (data.RootFeatures[i] == null)
                continue;
            var newFeature = Instantiate(data.RootFeatures[i]);
            card.RootFeatures.Add(newFeature);
        }

        card.CurrentPawn = pawn;
        if (card.ifHero)
        {
            pawn.ifHero = card.ifHero;
            pawn.Property.InitByCard(card);
            pawn.FeatureMono.InitFeaturesByCard(card);
        }
        else
            pawn.InitByCard();
        pawn.Roots.InitRootsByCard(card);
        return pawn;
    }
}
